package com.example.demo.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.example.demo.entity.Competition;
import com.example.demo.mapper.CompetitionMapper;

@Service
public class DcMainService {
@Autowired
CompetitionMapper competitionMapper;
//事务回滚
//查找所有比赛信息
@Transactional
public List<Competition> getAllCompetitionInfo() {
	try {
	return competitionMapper.getAllCompetitionInfo();
	}
	catch(Exception e){
		TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	}
	return competitionMapper.getAllCompetitionInfo();
}


//根据日期区间查找赛事
@Transactional
public List<Competition> queryByDate(String date1,String date2) throws java.text.ParseException{
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	Date StartTime=sdf.parse(date1);
	Date EndTime=sdf.parse(date2);
	try {
	return competitionMapper.queryByDate(StartTime,EndTime);	
	}
	catch(Exception e) {
		TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	}
	return competitionMapper.queryByDate(StartTime,EndTime);
}

//取消赛事申请
@Transactional
public void deleteCompetition(String OrganizersName,String CompetitionName,String CompetitionType,
		String CompetitionDate) {
	try {
	competitionMapper.deleteCompetition(OrganizersName, CompetitionName, CompetitionType, CompetitionDate);	
	}
	catch(Exception e) {
		TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	}
}

//新建赛事
@Transactional
public boolean newCompetition(String organizersName, String competitionName, String competitionType,
		String competitionDate, String competitionPrice, String competitionRemarks, String competitionStatus,
		String CreateTime) {
	try {
	int i=0;
	i=competitionMapper.newCompetition(organizersName, competitionName, competitionType, competitionDate, competitionPrice,competitionPrice,competitionStatus,CreateTime);
	if(i==1)
		return true;
	else
		return false;
	}
	catch(Exception e) {
		TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	}
	return true;
}
}
